﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class EmpInsuranceAdd : System.Web.UI.Page
    {
        EmpInsuranceBiz empInsuranceBiz = new EmpInsuranceBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        DefDicBiz defDicBiz=new DefDicBiz();
        string insuranceID = "0";//By Update
        string account = "";
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
              //  isSpecal = user.IsSpecial;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["InsuranceID"] != null)
            {
                insuranceID = Request.Params["InsuranceID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnEnd.Enabled = false;
                btnTransOk.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            { 
                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的保险信息吗？");
                defDicBiz.InitDropDownList("Admin_Insurance_Type", ddlTypeID, "养老保险");
                txtBeginTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
               // txtEndTime.Text = System.DateTime.Now.AddYears(1).ToString("yyyy-MM-dd");
                if (insuranceID != "0")
                {
                    BindData(insuranceID);
                }
                
            }
        }
        private void BindData(string insuranceID)
        {
            DataTable dt = empInsuranceBiz.GetOneInfo(insuranceID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();


                string BeginTime = dt.Rows[0]["BeginTime"].ToString();
                if (Utils.RegexRule.IsDate(BeginTime))
                    txtBeginTime.Text = Convert.ToDateTime(BeginTime).ToString("yyyy-MM-dd");
                else txtBeginTime.Text = BeginTime;

                string EndTime = dt.Rows[0]["EndTime"].ToString();
                if (Utils.RegexRule.IsDate(EndTime))
                    txtEndTime.Text = Convert.ToDateTime(EndTime).ToString("yyyy-MM-dd");
                else txtEndTime.Text = EndTime;

                string statu = dt.Rows[0]["Statu"].ToString();
                if (statu == "N")//过期
                {
                    if (AccountBiz.GetRange(account, "EmpInsuranceList.aspx") != RangeChar.Super)
                    {
                        btnUpdate.Enabled = false;
                        btnDelete.Enabled = false;
                    }
                    btnTransOk.Enabled = false;
                    btnClear.Enabled = false;
                    btnEnd.Enabled = false;                    
                }
                else if (statu == "Y") 
                    btnTransOk.Enabled = false; 
                //else if (statu == "T") statu = "临时";
               // else statu = "过期";
                defDicBiz.InitSynDropDownList2(ddlStatu, statu);

                defDicBiz.InitDropDownList(ddlTypeID, dt.Rows[0]["TypeID"].ToString());
                txtInsuranceNo.Text = dt.Rows[0]["InsuranceNo"].ToString();

                txtBaseValue.Text = dt.Rows[0]["BaseValue"].ToString();
                txtInsurancePeriod.Text = dt.Rows[0]["InsurancePeriod"].ToString();
                txtEmpPay.Text = dt.Rows[0]["EmpPay"].ToString();
                txtComPay.Text = dt.Rows[0]["ComPay"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
               
              
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string bTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "开始时间无效");
                return;
            }
            string eTime = txtEndTime.Text.Trim();
            if (!string.IsNullOrWhiteSpace(eTime))
            {
                if (!RegexRule.IsDate(eTime))
                {
                    JSUtility.Alert(this, "结束时间无效");
                    return;
                }
            }
            string baseValue = txtBaseValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(baseValue)) baseValue = "0";
            else
            {
                if (!RegexRule.IsDecimal(baseValue))
                {
                    JSUtility.Alert(this, "保险基数应该输入数字");
                    return;
                }

            }

            string insurancePeriod = txtInsurancePeriod.Text.Trim();
            if (!RegexRule.IsDecimal(insurancePeriod))
            {
                JSUtility.Alert(this, "周期应该输入数字");
                return;
            }
            string empPay = txtEmpPay.Text.Trim();
            if (!RegexRule.IsDecimal(empPay))
            {
                JSUtility.Alert(this, "个人支付金额应该输入数字");
                return;
            }
            string comPay = txtComPay.Text.Trim();
            if (!RegexRule.IsDecimal(comPay))
            {
                JSUtility.Alert(this, "企业支付金额应该输入数字");
                return;
            }
            
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("TypeID", ddlTypeID.SelectedValue);
            sdList.Add("InsuranceNo", txtInsuranceNo.Text.Trim());
            sdList.Add("InsurancePeriod", insurancePeriod);
            sdList.Add("BaseValue", baseValue);
            sdList.Add("EmpPay", empPay);
            sdList.Add("ComPay", comPay);
            sdList.Add("BeginTime", bTime);
            if (!string.IsNullOrWhiteSpace(eTime))
            {
                sdList.Add("EndTime", eTime);
            }
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Statu", ddlStatu.SelectedValue);
            sdList.Add("Account", account);


                JSUtility.Alert(this, empInsuranceBiz.Insert(sdList));
           // JSUtility.AlertAndRedirect(info, "EmpInsuranceAdd.aspx");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新权限！");
                    return;
                }
            }
            if (insuranceID == "0")
            {
                JSUtility.Alert(this, "无法操作，缺少数据");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string bTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "开始时间无效");
                return;
            }
            string eTime = txtEndTime.Text.Trim();
            if (!string.IsNullOrWhiteSpace(eTime))
            {
                if (!RegexRule.IsDate(eTime))
                {
                    JSUtility.Alert(this, "结束时间无效");
                    return;
                }
            }
            string baseValue = txtBaseValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(baseValue)) baseValue = "0";
            else
            {
                if (!RegexRule.IsDecimal(baseValue))
                {
                    JSUtility.Alert(this, "保险基数应该输入数字");
                    return;
                }

            }

            string insurancePeriod = txtInsurancePeriod.Text.Trim();
            if (!RegexRule.IsDecimal(insurancePeriod))
            {
                JSUtility.Alert(this, "周期应该输入数字");
                return;
            }
            string empPay = txtEmpPay.Text.Trim();
            if (!RegexRule.IsDecimal(empPay))
            {
                JSUtility.Alert(this, "个人支付金额应该输入数字");
                return;
            }
            string comPay = txtComPay.Text.Trim();
            if (!RegexRule.IsDecimal(comPay))
            {
                JSUtility.Alert(this, "企业支付金额应该输入数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("InsuranceID", insuranceID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("TypeID", ddlTypeID.SelectedValue);
            sdList.Add("InsuranceNo", txtInsuranceNo.Text.Trim());
            sdList.Add("InsurancePeriod", insurancePeriod);
            sdList.Add("BaseValue", baseValue);
            sdList.Add("EmpPay", empPay);
            sdList.Add("ComPay", comPay);
            sdList.Add("BeginTime", bTime);
            if (!string.IsNullOrWhiteSpace(eTime))
            {
                sdList.Add("EndTime", eTime);
            }
            else
                sdList.Add("EndTime", "null");
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Statu", ddlStatu.SelectedValue);
            sdList.Add("Account", account);

                JSUtility.Alert(this,empInsuranceBiz.Update(sdList,2));

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除权限！");
                    return;
                }
            }
            if (insuranceID != "0")
            {
                string empCode = txtEmpCode.Text.Trim();
                if (string.IsNullOrWhiteSpace(empCode))
                {
                    JSUtility.Alert(this, "员工编号必须填写");
                    return;
                }
                if (!employeeBiz.IsExistEmpCode(empCode))
                {
                    JSUtility.Alert(this, "员工编号不存在");
                    return;
                }
                JSUtility.Alert(this, empInsuranceBiz.DeleteOneInfo(insuranceID, empCode));
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }
       
        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (StringUtility.IncludeNumeric(empCode))
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职，不能再次离职");
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        }
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                            JSUtility.Alert(this, info);
                    }

                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnEnd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新权限！");
                    return;
                }
            }
            if (insuranceID == "0")
            {
                JSUtility.Alert(this, "无法操作，缺少数据");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string eTime = txtEndTime.Text.Trim();            
                if (!RegexRule.IsDate(eTime))
                {
                    JSUtility.Alert(this, "结束时间无效");
                    return;
                }
           

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("InsuranceID", insuranceID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("Statu", "N");
            
                sdList.Add("EndTime", eTime);
          
            sdList.Add("StatuTime", System.DateTime.Now.ToString());

                  JSUtility.Alert(this,empInsuranceBiz.Update(sdList,0));
                 
        }

        protected void btnTransOk_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpInsuranceAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpInsuranceList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新权限！");
                    return;
                }
            }
            if (insuranceID == "0")
            {
                JSUtility.Alert(this, "无法操作，缺少数据");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("InsuranceID", insuranceID);
            sdList.Add("EmpCode", txtEmpCode.Text.Trim());
            sdList.Add("Statu", "Y");
            sdList.Add("StatuTime", System.DateTime.Now.ToString());
                JSUtility.Alert(this, empInsuranceBiz.Update(sdList,1));
            //更新 employee
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect("EmpInsuranceAdd.aspx");
        }
    }
}